<template>
    <!-- 新增地址 -->
    <div class="addressEdit">
        <van-address-edit
            :address-info="info"
            :area-list="areaList"
            :show-delete="!!$route.query.id"
            show-set-default
            :area-columns-placeholder="['请选择', '请选择', '请选择3']"
            @save="onSave"
            @delete="onDelete"
        >
            <div class="van-cell van-field">
                <div class="van-cell__title van-field__label">
                    <span>标签</span>
                </div>
                <div class="van-cell__value van-field__value">
                    <div class="van-field__body">
                        <van-radio-group v-model="label" direction="horizontal">
                            <van-radio name="家">家</van-radio>
                            <van-radio name="公司">公司</van-radio>
                            <van-radio name="学校">学校</van-radio>
                        </van-radio-group>
                    </div>
                </div>
            </div>
        </van-address-edit>
    </div>
</template>

<script>
import { areaList } from "@vant/area-data";
import { Toast } from "vant"
import { reqAddressDel, reqAddressList, reqAddressEdit, reqAddressAdd } from "../../http/api"
export default {
    data() {
        return {
            areaList: areaList,
            info: {},
            label: "家",
        }
    },
    mounted() {
        if (this.$route.query.id) {
            reqAddressList({
                id: this.$route.query.id,
                uid: JSON.parse(localStorage.getItem("isLogin")).uid,
            }).then((res) => {
                if (res.data.code == 200) {
                    let info = res.data.list[0];
                    this.info = {
                        id: info.id,
                        name: info.username,
                        tel: info.userphone,
                        addressDetail: info.useraddress,
                        areaCode: info.regioncode,
                        isDefault: info.statue == 1 ? true : false,
                    }
                }
            })
        }
    },
    methods: {
        onSave(content) {
            if (this.$route.query.id) {
                //如果传了id就是编辑
                reqAddressEdit({
                    id: content.id,
                    username: content.name,
                    userphone: content.tel,
                    regioncode: content.areaCode,
                    location: content.province + content.city + content.county,
                    useraddress: content.addressDetail,
                    status: content.isDefault ? 1 : 0,
                    label: this.label,
                }).then((res) => {
                    if (res.data.code == 200) {
                        Toast(res.data.msg)
                        this.$router.replace("/a-list");
                    }
                })
            } else {
                //没有id就是地址添加
                reqAddressAdd({
                    uid: JSON.parse(localStorage.getItem("isLogin")).uid,
                    username: content.name,
                    userphone: content.tel,
                    regioncode: content.areaCode,
                    location: content.province + content.city + content.county,
                    useraddress: content.addressDetail,
                    status: content.isDefault ? 1 : 0,
                    label: this.label,
                }).then((res) => {
                    if (res.data.code == 200) {
                        Toast(res.data.msg);
                        this.$router.replace("/a-list")
                    }
                })
            }
        },
        onDelete() {
            reqAddressDel({
                id:this.$route.query.id
            }).then((res)=>{
                if(res.data.code==200){
                    Toast(res.data.msg);
                    this.$router.replace("/a-list")
                }
            })
        }
    }
}
</script>

<style scoped lang="less">
@import url(../../less/index.less);
.van-cell__value {
    display: flex;
    justify-content: space-around;
    font-size: 0.12rem;
}
</style>